package com.woniuxy.main.mapper;

import com.woniuxy.main.pojo.Staff;
import com.woniuxy.main.provider.StaffProvider;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

/*
 *
 *  @author zhc
 *  @create 2019-08-01 9:56
 */
@Repository
public interface StaffMapper {
    /*
    查找
     */
    @Select("select * from staff where flag=0")
    @Results({
            @Result(id=true,column="staff_id",property="staff_id"),
            @Result(column = "saccount", property = "saccount"),
            @Result(column="spassword",property="spassword"),
            @Result(column = "sname",property = "sname"),
            @Result(column = "sage",property = "sage"),
            @Result(column = "ssex",property = "ssex"),
            @Result(column = "sphone",property = "sphone"),
            @Result(column = "scard",property = "scard"),
            @Result(column = "role_id",property = "role",
                    one=@One(
                    select = "com.woniuxy.main.mapper.RolesMapper.findById"
            )),
            @Result(column = "department_id",property = "department",
                    one=@One(
                    select = "com.woniuxy.main.mapper.DepartmentMapper.findById"
            ))

    })
    List<Staff> findAll();
    /*
    添加
     */
    @Insert("insert into staff values(0,#{saccount},#{spassword},#{sname},#{sage},#{ssex},#{sphone},#{scard},#{role_id},#{department_id},0)")
    Boolean addStaff(Staff staff);

    /*

    通过account、精确查找
     */
    @Select("select * from staff where saccount=#{saccount} and flag=0")
    Staff selectStaffBySaccount(String saccount);

    /*
   通过staffid、精确查找
    */
    @Select("select * from staff where staff_id=#{id} and flag=0")
    Staff selectStaffById(int id);
    /*
    saccount重复查找
     */
    @Select("select count(*) from staff where saccount=#{saccoutn} and flag=0")
    int sRepeated(String saccount);
    /*
    更新
     */
    @Update("update staff set saccount=#{saccount},spassword=#{spassword},sname=#{sname},sage=#{sage},ssex=#{ssex},sphone=#{sphone},scard=#{scard},role_id=#{role_id},department_id=#{department_id} where staff_id=#{staff_id}")
    boolean updateStaff(Staff staff);

    /*
    软删除
     */
    @Update("update staff set flag=1 where staff_id=#{id}")
    boolean delStaff(int id);

    /*
    条件查找
     */
    @SelectProvider(type = StaffProvider.class, method = "provideSql")
    @Results({
            @Result(id = true, column = "staff_id", property = "staff_id"),
            @Result(column = "saccount", property = "saccount"),
            @Result(column = "spassword", property = "spassword"),
            @Result(column = "sname", property = "sname"),
            @Result(column = "sage", property = "sage"),
            @Result(column = "ssex", property = "ssex"),
            @Result(column = "sphone", property = "sphone"),
            @Result(column = "scard", property = "scard"),
            @Result(column = "role_id", property = "role",
                    one = @One(
                            select = "com.woniuxy.main.mapper.RolesMapper.findById"
                    )),
            @Result(column = "department_id", property = "department",
                    one = @One(
                            select = "com.woniuxy.main.mapper.DepartmentMapper.findById"
                    ))

    })
    List<Staff> findByCondition(Staff staff);


}
